﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Collections;
using System.Xml;


namespace GuiLogin
{
    public partial class guilogin : Form
    {
                
        public guilogin()
        {
            InitializeComponent();
            try
            {
                string passwordsa = "lcdor1990"; // sa password generate
                string passwordHashSha1 = CryptClass.Crypto.ComputeHash(passwordsa, "SHA1", null);
                MySqlConnection conexion = GuiLogin.Conexion.ObtenConexion();
                conexion.Open();
                if (conexion != null)
                {
                    using (MySqlCommand verificasa = GuiLogin.Conexion.Comandos("SELECT User FROM Users WHERE User='wctsa'", conexion))
                    using (MySqlDataReader readusr = verificasa.ExecuteReader())
                    {
                        ///    MessageBox.Show(passwordHashSha1);
                        ///    MessageBox.Show("Valor del campo User: {0}", readusr.GetString(0));
                    }
                    conexion.Close();
                }
                using (MySqlConnection newcon = GuiLogin.Conexion.ObtenConexion())
                {
                    newcon.Open();
                    using (MySqlCommand comando = new MySqlCommand("UPDATE Users SET Password='" + passwordHashSha1 + "' WHERE User='wctsa'", newcon))
                        comando.ExecuteNonQuery();

                    {
                        newcon.Close();
                    }
                }
            }
            catch
            {
                MessageBox.Show("Error, no se puede accesar a la base de datos, contacte al administrador");
                }
          
        }


        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            ActiveForm.Close();
        }

        private void guilogin_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string strpass;
                string passwdbox = CryptClass.Crypto.ComputeHash(textBox2.Text, "SHA1", null);
                MySqlConnection conexion = GuiLogin.Conexion.ObtenConexion();
                conexion.Open();
                if (conexion != null)
                {
                    using (MySqlCommand getusrdbpass = GuiLogin.Conexion.Comandos("SELECT Password FROM Users WHERE User='" + textBox1.Text + "'", conexion))
                    using (MySqlDataReader readpass = getusrdbpass.ExecuteReader())
                    {
                        readpass.Read();
                        strpass = readpass.GetString(0);
                        /// MessageBox.Show(strpass);
                        conexion.Close();
                        /// MessageBox.Show(CryptClass.Crypto.VerifyHash(textBox2.Text, "SHA1", strpass).ToString());
                        if (CryptClass.Crypto.VerifyHash(textBox2.Text, "SHA1", strpass) == true)
                        {
                            using (XmlWriter writer = XmlWriter.Create("C:/temp/actvusr.xml"))
                            {
                                writer.WriteStartDocument();
                                writer.WriteStartElement("usuario");
                                writer.WriteElementString("activo", textBox1.Text);
                                writer.WriteEndElement();
                                writer.WriteEndDocument();
                            }
                            this.DialogResult = DialogResult.OK;
                            this.Close();
                            guilogin.ActiveForm.Close();
                        }
                        else
                        {
                            MessageBox.Show("Error, usuario o password incorrecto");
                        }
                    }
                }
                // MessageBox.Show(passwdbox);
                // MessageBox.Show(textBox1.Text);
            }
            catch
            {
                MessageBox.Show("Error, usuario o password incorrecto");
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
       {
            this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckKeys);   
        }
        
        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            this.textBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckKeys);
        }
        private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                // Then Enter key was pressed
                //MessageBox.Show("tecla enter presionada");
                SendKeys.Send("{TAB}");
            }
        }
           
    }
}
